/*
	Copyright (C) 2010 Salil Bhagurkar

	This file is part of k3

	k3 is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	k3 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with k3.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <shared.h>

/*All functions used to debug are to be prepended by '--'*/
/*Prints out a tree generating path names for each fs node by tracking their parents, and then going to the links*/
void __fs_tree(struct fs *node)
{
	struct fs *t;
	int e;
	char tree_path[MAX_FS_NAME_LEN];
	t = node;
	e = fsiter_get_child(&t);
	while (!e) {
		tree_path[0] = '\0';
		printk("%s\n", fsparse_get_path(tree_path, t));
		__fs_tree(t);
		e = fsiter_get_next(&t);
	}
}


/*This debug function prints all information about an fs node*/
void __fs_print(struct fs *x)
{
	struct enumerator *e;
	struct listener *l;
	char name[MAX_FS_NAME_LEN];
	printk("Print : %s \n", x->name);
	printk("Flags = 0x%x\n", x->flags);
	u32 fstype = x->flags & FSFLAGS_TYPE_MASK;
	switch(fstype) {
	case FSFLAGS_DEVICE:
		printk("(device)\n");
		break;
	case FSFLAGS_DATA:
		printk("(data)\n");
		break;
	case FSFLAGS_DEVICE_DRIVER:
		printk("(device driver)\n");
		break;
	case FSFLAGS_VMDRV:
		printk("(vm driver)\n");
		break;
	}
	u32 fsmode = x->flags & FSFLAGS_MODE_MASK;
	switch(fsmode) {
	case FSFLAGS_MODE_READ:
		printk("(on read)\n");
		break;
	case FSFLAGS_MODE_WRITE:
		printk("(on write)\n");
		break;
	}
	if(x->system) {
		printk("System (%x) fs with%s%s supported\n", x->system, x->system->read==dummy?"":" read()", x->system->write==dummy?"":" write()");
		char path[MAX_FS_NAME_LEN];
		path[0] = '\0';
		if(x->system->fs) {
			printk("Base fs: %s\n", fsparse_get_path(path, x->system->fs));
		} else {
			printk("Base fs: Unsupported\n");
		}
		LIST_FOR(x->system->enumerators, e) {
			printk("Enumerator: %s\n", e->owner->name);
		}
		LIST_FOR(x->system->listeners, l) {
			printk("Listener: %s\n", l->owner->name);
		}
	}
	printk("Parent = %s, Has children: %s\n", x->parent->name, (x->links)?"Yes":"No");
	printk("Data size = %u\n", x->buf.len);
	printk("Data string = ");
	printk_buf(&x->buf);
	printk("\n");
}
